package com.filebackup.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.filebackup.dao.LocalFolderDao;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;

public class LocalFolderDaoImpl implements LocalFolderDao {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public boolean addVo(Vo vo) {
		System.out.println("dao success");
		
		this.getSessionFactory().getCurrentSession().save(vo);
		return false;
	}

	public boolean deleteVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().delete(vo);
		return false;
	}

	public List<Vo> findAll() {
		System.out.println("FIND ALL SUCCESS");
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFolderVo");
		List<Vo> voList = query.list();
		return voList;
	}

	public Vo findByPK(Object pk) {
		Vo vo = (Vo) this.getSessionFactory().getCurrentSession().get(LocalFolderVo.class, (Integer)pk);
		return vo;
	}

	public boolean updateVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().update(vo);
		return false;
	}

	public List<Vo> findByUpperId(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFolderVo where upperId=:upperId").setInteger("upperId", id);
		List<Vo> voList = query.list();
		return voList;
	}

	public List<Vo> findRootFolder(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFolderVo where ownerId=:ownerId and upperId=:upperId").setInteger("ownerId", id).setInteger("upperId", 0);
		List<Vo> voList = query.list();
		return voList;
	}

	public List<Vo> findByOwner(int id) {
		// TODO Auto-generated method stub
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from LocalFolderVo where ownerId=:ownerId").setInteger("ownerId", id);
		List<Vo> voList = query.list();
		return voList;
	}

	public int findIdByVo(LocalFolderVo vo) {
		// TODO Auto-generated method stub
		LocalFolderVo lvo=(LocalFolderVo)this.getSessionFactory().getCurrentSession().createQuery("from LocalFolderVo where folderName=:folderName and ownerId=:ownerId and upperId=:upperId").setString("folderName", vo.getFolderName()).setInteger("ownerId", vo.getOwnerId()).setInteger("upperId", vo.getUpperId()).uniqueResult();
		return lvo.getId();
	}
}
